import java.io.Serializable;

public class Query implements Serializable{
    private QueryType type = null;
    private String[] parameters = null;

    public Query(String query) {
        String words[] = query.split(" ");
        if (words[0].equals("add") && words[1].equals("master")) {
            type = QueryType.ADD_MASTER;
            parameters = getArrayParametersFromString(words, 2);
        } else if (words[0].equals("add") && words[1].equals("slave")) {
            type = QueryType.ADD_SLAVE;
            parameters = getArrayParametersFromString(words, 2);
        } else if (words[0].equals("put")) {
            type = QueryType.PUT;
            parameters = getArrayParametersFromString(words, 1);
        } else if (words[0].equals("get")) {
            type = QueryType.GET;
            parameters = getArrayParametersFromString(words, 1);
        } else if (words[0].equals("remove")) {
            type = QueryType.REMOVE;
            parameters = getArrayParametersFromString(words, 1);
        } else if (words[0].equals("update")) {
            type = QueryType.UPDATE;
            parameters = getArrayParametersFromString(words, 1);
        } else if (words[0].equals("move") && words[1].equals("data")) {
            type = QueryType.MOVE_DATA;
            parameters = getArrayParametersFromString(words, 2);
        }
    }

    private static String[] getArrayParametersFromString(String[] array, int head) {
        String values[] = new String[array.length - head];
        for (int i = 0; i < values.length; i++) {
            values[i] = array[i + head];
        }
        return values;
    }

    public QueryType getQueryType() {
        return type;
    }

    public String[] getQueryParameters() {
        return parameters;
    }
}
